package com.crm.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.DTO.BasePageQuery;
import com.crm.annotation.BusinessType;
import com.crm.annotation.Log;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.TableDataInfo;
import com.crm.common.utils.ThreadLocalUtil;
import com.crm.controller.BaseController;
import com.crm.entity.system.SysNotice;
import com.crm.service.system.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private ISysNoticeService noticeService;


    /**
     * @param noticeId
     * @return
     */
    @PutMapping("/{noticeId}")
    public AjaxResult oneRead(@PathVariable Long noticeId) {
        return AjaxResult.success(noticeService.oneRead(noticeId));
    }

    /**
     * 获取通知公告列表
     */
    @GetMapping("/pagelist/{status}")
    public TableDataInfo pageList(@PathVariable String status,BasePageQuery basePageQuery) {
        SysNotice notice = new SysNotice();
        notice.setStatus(status);
        Page<SysNotice> list = noticeService.selectNoticePage(notice, basePageQuery);
        return getDataTable(list);
    }

    /**
     * 获取通知公告列表
     */
    @GetMapping("/list/{status}")
    public TableDataInfo list(@PathVariable String status,BasePageQuery basePageQuery) {
        SysNotice notice = new SysNotice();
        notice.setStatus(status);
        Page<SysNotice> list = noticeService.selectNoticePage(notice, basePageQuery);
        return getDataTable(list);
    }


    /**
     * 根据通知公告编号获取详细信息
     */

    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */

    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(ThreadLocalUtil.getUserName());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */

    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(ThreadLocalUtil.getUserName());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */

    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }
}
